package com.datang.petstore.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-03 20:57
 * @notify
 * @version 1.0
 */

import com.datang.petstore.entity.InformCause;
import com.datang.petstore.generatemapper.InformCauseMapper;
import com.datang.petstore.generatemapper.PetMoldMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TestService {

    @Autowired
    private PetMoldMapper petMoldMapper;

    @Autowired
    private InformCauseMapper informCauseMapper;

    /*
     * 1 执行1service
     *   开启事务
     * 2 执行2service
     *  开启事务
     *  执行代码
     * 提交事务
     * 3 回到1service 抛出异常
     * 回滚代码((TestService) AopContext.currentProxy()).
     * */


    @Transactional
    public void service1() {
        InformCause petMold = null;
        try {
            petMold = new InformCause();
            petMold.setCause("哈哈");
            petMold.setManageId("haha ");
            petMold.setDate(new Date());
            throw new RuntimeException();
        } finally {
            informCauseMapper.insert(petMold);
        }
    }

    @Transactional
    public String service2() {
        InformCause petMold = new InformCause();
        petMold.setCause("哈哈");
        petMold.setManageId("haha ");
        petMold.setDate(new Date());
        informCauseMapper.insert(petMold);

        throw new RuntimeException();
    }
}
